﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Data;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;


namespace DotNetNuke.Modules.UserDefinedTable
{

  public class DataTypeDocument : DataType
  {



    protected struct FieldSettings
    {
      public string Title;
      public string OutputFormat;
      public bool Abbreviate;
    }




    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {

      System.Web.HttpContext.Current.Trace.Write("enter DataTypeDocument.RenderValuesToHtmlInsideDataSet");

      if (ds != null)
      {
        ArrayList fields = new ArrayList();
        DataTable tableData = ds.Tables[DataSetTableName.Data];
        DotNetNuke.Modules.UserDefinedTable.TokenReplace objTokenReplace = new DotNetNuke.Modules.UserDefinedTable.TokenReplace();
        objTokenReplace.ModuleId = ModuleId;

        foreach (DataRow row in ds.Tables[DataSetTableName.Fields].Rows)
        {
          if (Convert.ToString(row[FieldsTableColumn.Type]) == this.Name)
          {
            FieldSettings field;
            field.Title = Convert.ToString(row[FieldsTableColumn.Title]);
            field.OutputFormat = Utilities.CStrN((row[FieldsTableColumn.OutputSettings]), "");
            field.Abbreviate = Utilities.CBoolN((row[FieldsTableColumn.NormalizeFlag]), false);
            fields.Add(field);
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Url, typeof(string)));
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Caption, typeof(string)));
            tableData.Columns.Add(new DataColumn(field.Title + DataTableColumn.Appendix_Original, typeof(string)));
          }
        }

        if (fields.Count >= 1)
        {
          System.Web.HttpContext.Current.Trace.Write("DataTypeDocument.RenderValuesToHtmlInsideDataSet, field found");

          bool bolNewWindowSetting;
          PortalSettings PortalSettings = PortalController.GetCurrentPortalSettings();
          Hashtable settings = new ModuleController().GetModuleSettings(ModuleId);

          if (!settings.ContainsKey(SettingName.URLNewWindow))
            bolNewWindowSetting = false;

          else
            bolNewWindowSetting = bool.Parse(Convert.ToString(settings[SettingName.URLNewWindow]));

          FileController fc = new FileController();

          foreach (DataRow row in tableData.Rows)
          {
            foreach (FieldSettings field in fields)
            {
              string rawDBValue = Convert.ToString(row[field.Title]);

              if (!string.IsNullOrEmpty(rawDBValue))
              {
                System.Web.HttpContext.Current.Trace.Write("DataTypeDocument.RenderValuesToHtmlInsideDataSet, try render field");
                DocumentObject DocObj = new DocumentObject(ModuleId, rawDBValue);
                row[field.Title] = DocObj.ToLink();
                row[field.Title + DataTableColumn.Appendix_Caption] = "foo";
                row[field.Title + DataTableColumn.Appendix_Original] = DocObj.FileID;
                row[field.Title + DataTableColumn.Appendix_Url] = DocObj.ToUrl();
                System.Web.HttpContext.Current.Trace.Write("DataTypeDocument.RenderValuesToHtmlInsideDataSet, render field ok");
              }
            }
          }
        }
      }

      System.Web.HttpContext.Current.Trace.Write("DataTypeDocument.RenderValuesToHtmlInsideDataSet done");
    }




    public override DotNetNuke.Modules.UserDefinedTable.EditControl EditControl
    { get { return new EditDocument(); } }




    public override string Name
    { get { return "Document"; } }




    public override bool SupportsEditing
    { get { return true; } }




    public override bool SupportsNormalizeFlag
    { get { return true; } }




    public override bool SupportsValidation
    { get { return true; } }


  }
}

